package cate.game.role.task.msg;

import cate.common.table.d.GDFunc;
import cate.game.GameBody;
import cate.game.client.ClientContext;
import cate.game.client.msg.ClientMsg;
import cate.game.client.msg.MsgConfig;
import cate.game.client.msg.MsgInput;
import cate.game.client.msg.id.MsgIdBTask;
import cate.game.event.task.TaskGetRewardEvent;
import cate.game.role.task.TaskItem;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcResult;

@ClientMsg(title = "领取任务奖励 の 请求")
public class TaskGetRewardReq implements MsgInput {
    @NoteField(value = "任务组所属功能ID")
    public int funcId;

    @NoteField(value = "任务库里的任务ID")
    public int taskId;

    @Override
    public void execute(GameBody game, ClientContext cc) {
        EcResult<TaskItem> r = cc.getRole().getTask().getRewardDirect(funcId, taskId);
        if (r.ok()) {
            TaskItem item = r.data;
            cc.getRole().sendNow(new TaskGroupUpdateResp(item));
            if (funcId == GDFunc.MAINLINE) {
                // 如果是主线任务，那么删除该任务，接下来加入下一个任务
                cc.getRole().getTask().mainline.addNextTask();
            } else if (funcId == GDFunc.ACHIEVEMENT) {
                cc.getRole().getRes().achievement.addAchievementPoint(item);
                cc.getRole().getHistory().action.receiveAchievementReward();
            } else if (funcId == GDFunc.TASK_DAILY) {
                cc.getRole().getHistory().action.dailyTaskRewardGet(1);
            } else if (funcId == GDFunc.TASK_WEEKLY) {
                cc.getRole().getHistory().action.weeklyTaskRewardGet(1);
            }
            game.eventDispatcher.dispatchEvent(new TaskGetRewardEvent(cc.getRole(), item));
        } else {
            game.notice.message(cc.getRole(), r.message);
        }
    }


    @Override
    public MsgConfig conf() {
        return MsgIdBTask.GET_RWD_REQ;
    }
}
